package org.jeecg.modules.yc.web.config.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.yc.web.entity.Terminal;
import org.jeecg.modules.yc.web.entity.YcTerminalOnlineRate;
import org.jeecg.modules.yc.web.service.IPdDeviceService;
import org.jeecg.modules.yc.web.service.ITerminalService;
import org.jeecg.modules.yc.web.service.IYcTerminalOnlineRateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;

//@Component
public class CalculateOnlineRate {

    @Autowired
    private ITerminalService terminalService;
    @Autowired
    private IYcTerminalOnlineRateService terminalOnlineRateService;
    @Autowired
    private IPdDeviceService pdDeviceService;
//7200000
    @Scheduled(cron = "0 0 0-22/2 * * ?")
    public void performTask() {
        List<String> orgCodes = terminalService.selectOrgCode();
        for (String orgCode : orgCodes){
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("sys_org_code",orgCode);
            long terminalCount = terminalService.count(queryWrapper);
            // 获取当前时间
            LocalDateTime now = LocalDateTime.now();
            // 计算 2 小时前的时间
            LocalDateTime twoHoursAgo = now.minus(2, ChronoUnit.HOURS);
            queryWrapper.ge("last_connect_time", twoHoursAgo);
            long terminalOnlineCount = terminalService.count(queryWrapper);
            YcTerminalOnlineRate ycTerminalOnlineRate = new YcTerminalOnlineRate();
            ycTerminalOnlineRate.setTerminalCount((int)terminalCount);
            ycTerminalOnlineRate.setTerminalOnlineCount((int)terminalOnlineCount);
            ycTerminalOnlineRate.setOnlineRate(terminalCount == 0 ? 0: (int)(((double)terminalOnlineCount / terminalCount) * 100));
            ycTerminalOnlineRate.setSysOrgCode(orgCode);
            ycTerminalOnlineRate.setDeviceType(YcTerminalOnlineRate.TERMINAL);
            terminalOnlineRateService.save(ycTerminalOnlineRate);
            //传感器
            queryWrapper.clear();
            queryWrapper.eq("sys_org_code",orgCode);
            long ycPdDeviceCount = pdDeviceService.count(queryWrapper);
            queryWrapper.ge("communication_time",twoHoursAgo);
            long ycPdDeviceOnlineCount = pdDeviceService.count(queryWrapper);
            ycTerminalOnlineRate = new YcTerminalOnlineRate();
            ycTerminalOnlineRate.setTerminalCount((int)ycPdDeviceCount);
            ycTerminalOnlineRate.setTerminalOnlineCount((int)ycPdDeviceOnlineCount);
            ycTerminalOnlineRate.setOnlineRate(ycPdDeviceCount == 0 ? 0: (int)(((double)ycPdDeviceOnlineCount / ycPdDeviceCount) * 100));
            ycTerminalOnlineRate.setSysOrgCode(orgCode);
            ycTerminalOnlineRate.setDeviceType(YcTerminalOnlineRate.DEVICE);
            terminalOnlineRateService.save(ycTerminalOnlineRate);
        }
    }

}